﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;

namespace Homebound
{
    class TitleState : State
    {
        private const float TITLE_CHOICE_SEPERATION = 220f;
        private const float CHOICE_SEPERATION = 50f;
        public SpriteFont font;
        public SpriteFont titleFont;

        // Bool shift titles down only once
        public bool keyHeld = false;
        public bool start = true;

        // Background texture
        public Texture2D bg;

        // Logo texture
        private Texture2D logo;
        private Vector2 logoPosition = new Vector2(70, 70);
        private float hover = 0.0f;

        // BGM
        private Song bgm;

        public TitleState(SpriteFont newFont, SpriteFont newTitleFont, GraphicsDeviceManager newGraphics, GameServiceContainer newServices, ContentManager newContent)
            : base(newGraphics, newServices, newContent)
        {
            font = newFont;
            titleFont = newTitleFont;

            // Setup choices.
            choices.Add("Start");

            //choices.Add("Options");
            selectedChoice = 0;
        }

        // Called whenever a new state is selected.
        public override void onEnter(State state)
        {
            enterPressed = false;
            base.onEnter(state);

            // Load graphics content
            loadContent();

            // Play BGM
            MediaPlayer.Play(bgm);
            MediaPlayer.IsRepeating = true;
        }

        // Called when the state is changing.
        public override void onExit(State state)
        {
            base.onExit(state);
        }

        // Draw the visual representation of the state.
        public override void draw(SpriteBatch spriteBatch, GameTime gameTime, Vector2 screenSize)
        {
            int i;

            spriteBatch.Begin();

            // Display the background.
            spriteBatch.Draw(bg, Vector2.Zero, Color.White);

            spriteBatch.End();

            // Draw a planet
            planets.DrawTitlePlanet();

            spriteBatch.Begin();

            // Draw the logo
            Rectangle r = new Rectangle(0, 0, logo.Width, logo.Height); 
            spriteBatch.Draw(logo, logoPosition, r, Color.White, 0, Vector2.Zero, 0.5f,SpriteEffects.None ,0);

            // Darw title
            spriteBatch.DrawString(titleFont, "Homebound", new Vector2(screenSize.X / 2, 0), Color.White);

            // Display the choices.
            for (i = 0; i < choices.Count(); i++)
            {
                if (i == selectedChoice) // Is the current choice being printed.
                    spriteBatch.DrawString(font, choices[i], new Vector2(screenSize.X / 2, TITLE_CHOICE_SEPERATION + (i + 1) * CHOICE_SEPERATION), Color.Red);
                else // The current choice is not being printed.
                    spriteBatch.DrawString(font, choices[i], new Vector2(screenSize.X / 2, TITLE_CHOICE_SEPERATION + (i + 1) * CHOICE_SEPERATION), Color.Yellow);
            }
                
            spriteBatch.End();
            base.draw(spriteBatch, gameTime, screenSize);
        }

        // Handles all input for the current state, (Keyboard, GamePad).
        public override void input(GameTime gameTime, List<Keys> keys, GamePadButtons buttons, GamePadDPad dpad, GamePadThumbSticks thumbSticks)
        {
            if ((Keyboard.GetState().IsKeyDown(Keys.Up) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A)) && !keyHeld) // The up button was pressed.
            {
                selectedChoice = (selectedChoice - 1) % choices.Count();

                if (selectedChoice < 0)
                    selectedChoice = choices.Count() - 1;

                // Key has been pressed
                keyHeld = true;
            }
            else if ((Keyboard.GetState().IsKeyDown(Keys.Down) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back)) && !keyHeld) // The down button was pressed.
            {
                selectedChoice = (selectedChoice + 1) % choices.Count();

                // Key has been pressed
                keyHeld = true;
            }

            // Reset key press state
            if (Keyboard.GetState().IsKeyUp(Keys.Up) && Keyboard.GetState().IsKeyUp(Keys.Down) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadUp) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadDown)) 
            {
                keyHeld = false;
            }

            base.input(gameTime, keys, buttons, dpad, thumbSticks);
        }

        // Handles all update operations for the current state, (Keyboard, GamePad).
        public override void update(GameTime gameTime)
        {
            // Hover Logo
            hover += 0.01f % MathHelper.ToRadians(360);
            logoPosition.Y += 0.25f * (float)Math.Sin(hover);
            // Update Planet
            planets.UpdateTitlePlanet(gameTime);

            base.update(gameTime);
        }

        // Conditions necessary to enter go return to a previous state.
        public override bool goingBack(State next)
        {
            return false;
        }

        // Conditions necessary to enter go to a next state.
        public override bool goingForward(State prev)
        {
            GameOverState gameOverState = (GameOverState)prev;

            if (gameOverState.reset || prev.enterPressed)
            {
                //keyHeld = true;
                return true;
            }
            else if (!gameOverState.reset || !prev.enterPressed)
            {
                keyHeld = false;
                return false;
            }
            return false;
        }

        // Load all content for the current state.
        public override void loadContent()
        {
            // Load background image
            bg = content.Load<Texture2D>("nebula_1");

            // Load logo
            logo = content.Load<Texture2D>("rocket_logo");

            // Load BGM
            bgm = content.Load<Song>("Sounds/It Never Stops Raining Here");
        }
    }
}
